package com.heima.config;

import cn.hutool.core.util.ObjectUtil;
import com.heima.api.fallback.ItemClientFallbackFactory;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

public class DefaultFeignConfig {
    // 定义日志级别
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    // FallbackClass 交给IOC容器管理
//    @Bean
//    public ItemClientFallbackClass itemClientFallbackClass(){
//        return new ItemClientFallbackClass();
//    }

    // FallbackFactory<T> 交给IOC容器管理
    @Bean
    public ItemClientFallbackFactory itemClientFallbackFactory() {

        return new ItemClientFallbackFactory();
    }

    // 创建拦截器对象
    public RequestInterceptor userRequestInterceptor() {
        return requestTemplate -> {
            // ThreadLocal
            Long user = UserContext.getUser();
            if (ObjectUtil.isNotEmpty(user)) {
                requestTemplate.header("user-info", Arrays.asList(user.toString()));
            }
        };
    }
}
